package br.com.salao.model;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import org.apache.log4j.Logger;

import br.com.salao.entity.RatingEstabelecimento;
import br.com.salao.session.RatingEstabelecimentoBase;
import br.com.salao.util.Utils;

public class RatingEstabelecimentoModel  {

	private Logger logger = Logger.getLogger(RatingEstabelecimentoModel.class.getName());
	private RatingEstabelecimentoBase ratingEstabelecimentoBase;
	
    public RatingEstabelecimentoBase getBean() throws Exception {
    	
        Properties prop = Utils.getProperties();     
        Context ctx = new InitialContext(prop); 
        Object ref = ctx.lookup(prop.getProperty("RATING_LOOKUP"));       
      
        return (RatingEstabelecimentoBase)PortableRemoteObject.narrow(ref, RatingEstabelecimentoBase.class);
    }

	public RatingEstabelecimento buscarRatingPorId(int id) throws Exception {
		try {
			ratingEstabelecimentoBase = getBean();
			return ratingEstabelecimentoBase.buscarRatingPorId(id);
		} catch (Exception e) {
			StringBuffer sb = new StringBuffer();
			sb.append("Mensagem: " + e.getMessage() + "\n");
			sb.append("Causa: " + e.getCause() + "\n");
			sb.append(e.getStackTrace());
			
			logger.error(sb.toString(),e);
			e.printStackTrace();
			throw e;
		}
	}

	public RatingEstabelecimento inserir(RatingEstabelecimento ratingEstabelecimento) throws Exception{
		try {
			ratingEstabelecimentoBase = getBean();
			return ratingEstabelecimentoBase.inserir(ratingEstabelecimento);
		} catch (Exception e) {
			StringBuffer sb = new StringBuffer();
			sb.append("Mensagem: " + e.getMessage() + "\n");
			sb.append("Causa: " + e.getCause() + "\n");
			sb.append(e.getStackTrace());
			
			logger.error(sb.toString(),e);
			e.printStackTrace();
			throw e;
		}
	}

	public RatingEstabelecimento update(RatingEstabelecimento ratingEstabelecimento) throws Exception{
		try {
			ratingEstabelecimentoBase = getBean();
			return ratingEstabelecimentoBase.update(ratingEstabelecimento);
		} catch (Exception e) {
			StringBuffer sb = new StringBuffer();
			sb.append("Mensagem: " + e.getMessage() + "\n");
			sb.append("Causa: " + e.getCause() + "\n");
			sb.append(e.getStackTrace());
			
			logger.error(sb.toString(),e);
			e.printStackTrace();
			throw e;
		}
	}	
	
}
